package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.StudyPlanDTO;
import com.xuedongzhilian.service.IStudyPlanService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学习计划控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/study/plan")
public class StudyPlanController {

    @Resource
    private IStudyPlanService studyPlanService;

    /**
     * 创建学习计划
     */
    @PostMapping
    public Result createStudyPlan(@RequestBody StudyPlanDTO studyPlanDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        studyPlanDTO.setUserId(userId);
        boolean success = studyPlanService.createStudyPlan(studyPlanDTO);
        return success ? Result.ok() : Result.fail("创建学习计划失败");
    }

    /**
     * 获取用户的学习计划列表
     */
    @GetMapping
    public Result getStudyPlans() {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        List<StudyPlanDTO> studyPlans = studyPlanService.getStudyPlansByUserId(userId);
        return Result.ok(studyPlans);
    }

    /**
     * 获取学习计划详情
     */
    @GetMapping("/{planId}")
    public Result getStudyPlanDetail(@PathVariable("planId") Long planId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        StudyPlanDTO studyPlanDTO = studyPlanService.getStudyPlanDetail(planId, userId);
        return studyPlanDTO != null ? Result.ok(studyPlanDTO) : Result.fail("学习计划不存在");
    }

    /**
     * 更新学习计划
     */
    @PutMapping
    public Result updateStudyPlan(@RequestBody StudyPlanDTO studyPlanDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        studyPlanDTO.setUserId(userId);
        boolean success = studyPlanService.updateStudyPlan(studyPlanDTO);
        return success ? Result.ok() : Result.fail("更新学习计划失败");
    }

    /**
     * 删除学习计划
     */
    @DeleteMapping("/{planId}")
    public Result deleteStudyPlan(@PathVariable("planId") Long planId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = studyPlanService.deleteStudyPlan(planId, userId);
        return success ? Result.ok() : Result.fail("删除学习计划失败");
    }

    /**
     * 完成学习计划
     */
    @PostMapping("/{planId}/complete")
    public Result completeStudyPlan(@PathVariable("planId") Long planId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = studyPlanService.completeStudyPlan(planId, userId);
        return success ? Result.ok() : Result.fail("完成学习计划失败");
    }
}